{% extends 'layout.html' %}

{% block content %}
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
                添加任务
            </div>
            <div class="panel-body">

                <form id="formAdd">

                    <div class="clearfix">
                        {% for obj in form %}
                            <div class="col-xs-6">
                                <div class="form-group" style="position: relative;margin-bottom: 20px">
                                    <label>{{ obj.label }}</label>
                                    {{ obj }}
                                    <span class="error-msg" style="color: red;position: absolute"></span>
                                </div>
                            </div>
                        {% endfor %}
                    </div >

                    <div class="col-xs-12">
                        <button id="btnAdd" type="button" class="btn btn-primary">提 交</button>
                    </div>
                </form>
            </div>
        </div>

        <div class="panel panel-default">
            <!-- Default panel contents -->
            <div class="panel-heading">
                <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
                任务列表
            </div>

            <!-- Table -->
            <table class="table table-bordered table-striped table-hover">
                <thead>
                <tr>
                    <th>序号</th>
                    <th>ID</th>
                    <th>标题</th>
                    <th>级别</th>
                    <th>负责人</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for obj in queryset %}
                    <tr>
                        <th>{{ forloop.counter }}</th>
                        <td>{{ obj.id }}</td>
                        <td>{{ obj.title }}</td>
                        <td>{{ obj.get_level_display }}</td>
                        <td>{{ obj.user.username }}</td>
                        <td>
                            <a href="/admin/{{ obj.id }}/edit" class="btn btn-primary btn-xs">编辑</a>
                            <a href="/admin/{{ obj.id }}/delete" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>

        <ul class='pagination'>
            {{ page_string }}
        </ul>
    </div>
{% endblock content %}

{% block js %}
    <script type="text/javascript">
        $(function (){
            // 页面框架加载完成之后代码自动执行
            bindBtnAddEvent();
        })

        function bindBtnAddEvent(){
            $("#btnAdd").click(function (){
                $(".error-msg").empty();

                $.ajax({
                    url: '/task/add/',
                    type: "post",
                    data: $("#formAdd").serialize(),
                    dataType: "JSON",
                    success: function (res){
                        if (res.status){
                            alert("添加成功");
                            // 用JS实现页面的刷新
                            location.reload();
                        }else {
                            $.each(res.error, function (name,data){
                                $("#id_" + name).next().text(data[0])
                            })
                        }
                    }
                })
            })
        }
    </script>
{% endblock js %}